package com.lanchewuyou.user.driver.bean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 *4S店数据库类
 */
public class FoursDB {
    private static final String FOURS_DB_NAME = "fourslist";
    private static final int FOURS_VERSION = 1;
    private static FoursDB foursDB;
    private SQLiteDatabase db;


    private FoursDB(Context context) {
        FourSOpenHelper dbHelper = new FourSOpenHelper(context, FOURS_DB_NAME, null, FOURS_VERSION);
        db = dbHelper.getWritableDatabase();
    }

    public synchronized static FoursDB getInstance(Context context) {
        if (foursDB == null) {
            foursDB = new FoursDB(context);

        }
        return foursDB;
    }
    public void wipeFours(){
        db.execSQL("DELETE FROM Fours");
    }

    public void saveFours(FourS fourS) {
        if (fourS != null) {
            ContentValues values = new ContentValues();
            values.put("fours_id", fourS.getId());
            values.put("fours_name", fourS.getName());
            values.put("fours_address", fourS.getAddress());
            values.put("fours_shortname", fourS.getShortname());
            values.put("fours_regional", fourS.getRegional());
            values.put("fours_brandid", fourS.getBrandid());
            db.insert("Fours", null, values);
        }

    }
    public List<FourS> findFours(){
        List<FourS> list=new ArrayList<FourS>() ;
        Cursor cursor=db.query("Fours",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                FourS fourS=new FourS();
                fourS.setId(cursor.getString(cursor.getColumnIndex("fours_id")));
                fourS.setName(cursor.getString(cursor.getColumnIndex("fours_name")));
                fourS.setAddress(cursor.getString(cursor.getColumnIndex("fours_address")));
                fourS.setShortname(cursor.getString(cursor.getColumnIndex("fours_shortname")));
                fourS.setBrandid(cursor.getString(cursor.getColumnIndex("fours_brandid")));
                fourS.setRegional(cursor.getString(cursor.getColumnIndex("fours_regional")));
                list.add(fourS);
            }while (cursor.moveToNext());
        }
        if(cursor!=null){
            cursor.close();
        }
        return  list;
    }

}
